<template>
	<globalModal
		:visible="visible"
		:width="500"
		:bodyStyle="bodyStyle"
		:zIndex="1061"
		:confirmLoading="confirmLoading"
		@ok="handleOk"
		@cancel="handleCancel"
	>
		<div slot="title">
			<template v-if="isBatch">
				给<span style="color: #f00">即兴问答-({{ testInfo.topicType | filterTopicTypeName }})</span>
				学生评分
			</template>
			<template v-else>
				给
				<span style="color: #f00"
					>({{ studList.length > 0 ? `给${studList.length}个学生` : stud.studName }})</span
				>
				评分
			</template>
		</div>
		<a-spin :spinning="confirmLoading">
			<div class="flex flex-pack-center flex-align-center" v-if="isBatch">
				<div style="font-weight: 600">批量评分的学生：</div>
				<a-radio-group v-model="batchType">
					<a-radio :style="radioStyle" :value="1">排名前五</a-radio>
					<a-radio :style="radioStyle" :value="2">回答正确</a-radio>
					<a-radio :style="radioStyle" :value="3">已作答</a-radio>
				</a-radio-group>
			</div>
			<div class="flex flex-pack-center" style="padding-top: 25px">
				<rate @change="changeNum"></rate></div
		></a-spin>
	</globalModal>
</template>

<script>
import { postActionExport, sysManageapi, postActionUpload } from '@/api/index.js';
import { ModalMixins } from '@/mixins';
import { mapState, mapMutations } from 'vuex';
import rate from '@/components/rate.vue';
import { topicTypeListEnum } from '@/utils/enumeration.js';
const newtopicTypeListEnum = topicTypeListEnum.concat([
	{
		group: '1',
		value: 99,
		text: '答题测试',
	},
]);
export default {
	name: 'praiseModal',
	mixins: [ModalMixins],
	components: { rate },
	// props: {
	// 	classId: {
	// 		type: String,
	// 		default: '',
	// 	},
	// },
	data() {
		return {
			bodyStyle: {
				maxHeight: '450px',
				minHeight: '100px',
				overflow: 'auto',
			},
			type: '',
			csId: '',
			classId: '',
			score: 0,
			stud: {},
			studList: [],
			radioStyle: {
				height: '30px',
				lineHeight: '30px',
			},
			batchType: 1,
			isBatch: false,
			testInfo: {},
		};
	},
	computed: {
		...mapState({
			deviceType: (state) => state.websockStore.deviceType,
		}),
	},
	methods: {
		//type 抢答 1；随机抽答 2，其他类型  3
		show(stud, type, csId, classId, isBatch) {
			this.visible = true;
			this.score = 0;
			this.stud = {};
			this.studList = [];
			this.isBatch = isBatch || 0;
			console.log('stud', stud, isBatch);
			if (isBatch) {
				//stud 即兴问答数据
				this.testInfo = stud;
			} else {
				if (Array.isArray(stud)) {
					this.studList = stud;
				} else {
					this.stud = stud;
				}
			}

			this.type = type;
			this.csId = csId;
			this.classId = classId;
		},

		handleCancel() {
			this.visible = false;
		},
		changeNum(num) {
			this.score = num;
		},
		//提交保存数据
		handleOk() {
			this.confirmLoading = true;
			let studIds = [];
			if (this.isBatch) {
				if (this.batchType == 1 && this.testInfo.countData.ranklist.length > 0) {
					studIds = this.testInfo.countData.ranklist.map((item) => item.studId);
				} else if (this.batchType == 2) {
					let index = this.testInfo.countData.answerList.findIndex((item) => item.name == '回答正确');
					if (index > -1 && this.testInfo.countData.answerList[index].userList.length > 0) {
						studIds = this.testInfo.countData.answerList[index].userList.map((item) => item.studId);
					}
				} else if (this.batchType == 3) {
					let list = this.testInfo.countData.answerList.filter((item) => item.name != '未提交');
					if (list.length > 0) {
						list.forEach((item) => {
							if (item.userList.length > 0) {
								studIds = studIds.concat(item.userList.map((item) => item.studId));
							}
						});
					}
				}
			} else {
				studIds = this.studList.length > 0 ? this.studList : [this.stud.studId];
			}
			if (studIds.length == 0) {
				this.$message.error('没有可以被表扬的学生');
				this.confirmLoading = false;
				return;
			}
			this.$postAction(this.$sysManageapi.answerUrl.addScore, {
				score: this.score,
				csId: this.csId,
				classId: this.classId,
				type: this.type,
				studIds: studIds,
			})
				.then((da) => {
					if (da.code == 1) {
						this.$message.success('评价成功');
						this.$emit('ok');
						this.handleCancel();
					}
				})
				.finally(() => {
					this.confirmLoading = false;
				});
		},
	},
	filters: {
		filterTopicTypeName: function (value, list) {
			if (value || value == 0) {
				let index = topicTypeListEnum.findIndex((item) => item.value == value);
				if (index > -1) {
					return topicTypeListEnum[index].text;
				} else {
					return '';
				}
			} else {
				return '';
			}
		},
	},
};
</script>

<style></style>
